package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private PermissionMapper permissionMapper;


	@Override
	public User findUserByUsername(String username) {
		//查询用户的基本信息，不包括角色和权限信息
		User user = userMapper.findUserByUsername(username);
		//拿到用户id去查中间表和role表，查出role
		Integer userId = user.getId();
		Set<Role> roles = roleMapper.findRoles(userId);
		user.setRoles(roles);
		//遍历role，拿到permission
		for (Role role : roles) {
			Integer roleId = role.getId();

			Set<Permission> permissions = permissionMapper.findPermissions(roleId);
			role.setPermissions(permissions);

		}


		return user;
	}

	@Override
	public List<Role> findAllRole() {
		List<Role> roleList = roleMapper.findAllRole();

		return roleList;
	}

	@Override
	public void add(User user, Integer[] roleIds) {
		//对密码进行加密
		String password = user.getPassword();
		BCryptPasswordEncoder bcypt = new BCryptPasswordEncoder();
		String pwd = bcypt.encode(password);
		System.out.println(pwd);
		user.setPassword(pwd);
		//user表进行添加
		userMapper.add(user);

		//对与role的中间表进行添加
		Integer userId = user.getId();
		for (Integer roleId : roleIds) {
			Map<String,Integer> map = new HashMap<>();
			map.put("userId",userId);
			map.put("roleId",roleId);
			userMapper.addRelat4Role(map);
		}

	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//
		PageHelper.startPage(currentPage,pageSize);
		//
		Page<User> page = userMapper.queryByCondition(queryString);


		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public User findById(Integer id) {
		//先查出user
		User user = userMapper.findById(id);
		//再用userId查出该用户所拥有的role
		Integer userId = user.getId();
		Set<Role> roleList = roleMapper.findRoles(userId);
		user.setRoles(roleList);

		return user;
	}

	@Override
	public void update(User user, Integer[] roleIds) {
		Integer userId = user.getId();
		//1.删除与role的中间表
		userMapper.deleteRelat4Role(userId);
		//2.对与role的中间表进行添加
		for (Integer roleId : roleIds) {
			Map<String,Integer> map = new HashMap<>();
			map.put("userId",userId);
			map.put("roleId",roleId);
			userMapper.addRelat4Role(map);
		}
		//3.修改user表
		userMapper.update(user);




	}

	@Override
	public void delete(Integer id) {
		//1.先删除与role关联的中间表
		userMapper.deleteRelat4Role(id);
		//2.再删除user表
		userMapper.delete(id);

	}


}
